package com.dqyt.app.service;

import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.ManufacturerBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.ManufacturerMapper;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class AppManufacturerService {
    @Autowired
    private ManufacturerMapper manufacturerMapper;

    /**
     * 查询制造厂商列表
     * @param request
     * @return
     */
    public ResultMsg getManufacturers(HttpServletRequest request,long userId,String token){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            //查询
            List<ManufacturerBean> data  = manufacturerMapper.getManufacturers();
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 添加制造厂商
     * @param request
     * @return
     */
    public ResultMsg addManufacturer(HttpServletRequest request,long userId,String token,String name,String address){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            //查询名称制造厂商
            List<ManufacturerBean>  ms =  manufacturerMapper.getManufacturersByName(name);
            if(ms.size()>0){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("该生产厂商已经存在");
                return ret;
            }
            User  muser=  UserInfoUtil.getUserInfologin(userId, token);
            ManufacturerBean manufacturerBean = new ManufacturerBean();
            manufacturerBean.setName(name);
            manufacturerBean.setEnname("");
            manufacturerBean.setAddress("");
            long tt = TimeUtil.currentTimeMillis();
            manufacturerBean.setAddTime(tt);
            manufacturerBean.setAddUser(muser.getUserId()+"");
            manufacturerBean.setUpdateTime(tt);
            manufacturerBean.setUpdateUser(muser.getUserId()+"");
            //新增制造厂商
           int res =  manufacturerMapper.addManufacturers(manufacturerBean);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("添加成功");
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




}
